<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>序列化使用 | 课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C03-Serializer/Deserializing.html" />
    
    
    <link rel="prev" href="../C03-Serializer/Declaring.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.2"
        data-chapter-title="序列化使用"
        data-filepath="C03-Serializer/Serializing.md"
        data-basepath=".."
        data-revision="Thu May 10 2018 07:57:11 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-IntroduceToDRF/index.html">
            
                
                    <a href="../C01-IntroduceToDRF/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入DjangoRESTframework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
            
                
                    <a href="../C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-IntroduceToDRF/IntroduceToREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/IntroduceToREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-IntroduceToDRF/HowToDesignREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/HowToDesignREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        RESTful设计方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
            
                
                    <a href="../C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用Django开发REST接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
            
                
                    <a href="../C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-IntroduceToDRF/AboutDRF.html">
            
                
                    <a href="../C01-IntroduceToDRF/AboutDRF.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DjangoRESTframework简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-DRFProject/index.html">
            
                
                    <a href="../C02-DRFProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        DRF工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-DRFProject/InstallAndConfig.html">
            
                
                    <a href="../C02-DRFProject/InstallAndConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-DRFProject/TheFirstDRFProgram.html">
            
                
                    <a href="../C02-DRFProject/TheFirstDRFProgram.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        见识DRF的魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Serializer/index.html">
            
                
                    <a href="../C03-Serializer/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Serializer序列化器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Serializer/Declaring.html">
            
                
                    <a href="../C03-Serializer/Declaring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        定义Serializer
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="C03-Serializer/Serializing.html">
            
                
                    <a href="../C03-Serializer/Serializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Serializer/Deserializing.html">
            
                
                    <a href="../C03-Serializer/Deserializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        反序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Serializer/ModelSerializer.html">
            
                
                    <a href="../C03-Serializer/ModelSerializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-View/index.html">
            
                
                    <a href="../C04-View/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-View/RequestAndResponse.html">
            
                
                    <a href="../C04-View/RequestAndResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Request与Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-View/View.html">
            
                
                    <a href="../C04-View/View.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        视图概览
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-View/ViewIntroduction.html">
            
                
                    <a href="../C04-View/ViewIntroduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        视图说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-View/ViewSet.html">
            
                
                    <a href="../C04-View/ViewSet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        视图集ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-View/Routers.html">
            
                
                    <a href="../C04-View/Routers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Components/index.html">
            
                
                    <a href="../C05-Components/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        其他功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Components/Authentication.html">
            
                
                    <a href="../C05-Components/Authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        认证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Components/Permissions.html">
            
                
                    <a href="../C05-Components/Permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        权限
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Components/Throttling.html">
            
                
                    <a href="../C05-Components/Throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        限流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Components/Filtering.html">
            
                
                    <a href="../C05-Components/Filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        过滤
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="C05-Components/Pagination.html">
            
                
                    <a href="../C05-Components/Pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="C05-Components/Versioning.html">
            
                
                    <a href="../C05-Components/Versioning.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        版本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="C05-Components/Exceptions.html">
            
                
                    <a href="../C05-Components/Exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="C05-Components/Documents.html">
            
                
                    <a href="../C05-Components/Documents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自动生成接口文档
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5E8F;&#x5217;&#x5316;&#x4F7F;&#x7528;">&#x5E8F;&#x5217;&#x5316;&#x4F7F;&#x7528;</h1>
<p>&#x6211;&#x4EEC;&#x5728;django shell&#x4E2D;&#x6765;&#x5B66;&#x4E60;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-shell">python manage.py shell
</code></pre>
<h2 id="1-&#x57FA;&#x672C;&#x4F7F;&#x7528;">1 &#x57FA;&#x672C;&#x4F7F;&#x7528;</h2>
<p>1&#xFF09; &#x5148;&#x67E5;&#x8BE2;&#x51FA;&#x4E00;&#x4E2A;&#x56FE;&#x4E66;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> booktest.models <span class="hljs-keyword">import</span> BookInfo

book = BookInfo.objects.get(id=<span class="hljs-number">2</span>)
</code></pre>
<p>2&#xFF09; &#x6784;&#x9020;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> booktest.serializers <span class="hljs-keyword">import</span> BookInfoSerializer

serializer = BookInfoSerializer(book)
</code></pre>
<p>3&#xFF09;&#x83B7;&#x53D6;&#x5E8F;&#x5217;&#x5316;&#x6570;&#x636E;</p>
<p>&#x901A;&#x8FC7;data&#x5C5E;&#x6027;&#x53EF;&#x4EE5;&#x83B7;&#x53D6;&#x5E8F;&#x5217;&#x5316;&#x540E;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-python">serializer.data
<span class="hljs-comment"># {&apos;id&apos;: 2, &apos;btitle&apos;: &apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;, &apos;bpub_date&apos;: &apos;1986-07-24&apos;, &apos;bread&apos;: 36, &apos;bcomment&apos;: 40, &apos;image&apos;: None}</span>
</code></pre>
<p>4&#xFF09;&#x5982;&#x679C;&#x8981;&#x88AB;&#x5E8F;&#x5217;&#x5316;&#x7684;&#x662F;&#x5305;&#x542B;&#x591A;&#x6761;&#x6570;&#x636E;&#x7684;&#x67E5;&#x8BE2;&#x96C6;QuerySet&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6DFB;&#x52A0;<strong>many=True</strong>&#x53C2;&#x6570;&#x8865;&#x5145;&#x8BF4;&#x660E;</p>
<pre><code class="lang-python">book_qs = BookInfo.objects.all()
serializer = BookInfoSerializer(book_qs, many=<span class="hljs-keyword">True</span>)
serializer.data
<span class="hljs-comment"># [OrderedDict([(&apos;id&apos;, 2), (&apos;btitle&apos;, &apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;), (&apos;bpub_date&apos;, &apos;1986-07-24&apos;), (&apos;bread&apos;, 36), (&apos;bcomment&apos;, 40), (&apos;image&apos;, N]), OrderedDict([(&apos;id&apos;, 3), (&apos;btitle&apos;, &apos;&#x7B11;&#x50B2;&#x6C5F;&#x6E56;&apos;), (&apos;bpub_date&apos;, &apos;1995-12-24&apos;), (&apos;bread&apos;, 20), (&apos;bcomment&apos;, 80), (&apos;image&apos;ne)]), OrderedDict([(&apos;id&apos;, 4), (&apos;btitle&apos;, &apos;&#x96EA;&#x5C71;&#x98DE;&#x72D0;&apos;), (&apos;bpub_date&apos;, &apos;1987-11-11&apos;), (&apos;bread&apos;, 58), (&apos;bcomment&apos;, 24), (&apos;ima None)]), OrderedDict([(&apos;id&apos;, 5), (&apos;btitle&apos;, &apos;&#x897F;&#x6E38;&#x8BB0;&apos;), (&apos;bpub_date&apos;, &apos;1988-01-01&apos;), (&apos;bread&apos;, 10), (&apos;bcomment&apos;, 10), (&apos;im&apos;, &apos;booktest/xiyouji.png&apos;)])]</span>
</code></pre>
<h2 id="2--&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x5D4C;&#x5957;&#x5E8F;&#x5217;&#x5316;">2  &#x5173;&#x8054;&#x5BF9;&#x8C61;&#x5D4C;&#x5957;&#x5E8F;&#x5217;&#x5316;</h2>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x5E8F;&#x5217;&#x5316;&#x7684;&#x6570;&#x636E;&#x4E2D;&#x5305;&#x542B;&#x6709;&#x5176;&#x4ED6;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x5BF9;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x6570;&#x636E;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x9700;&#x8981;&#x6307;&#x660E;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF0C;&#x5728;&#x5B9A;&#x4E49;&#x82F1;&#x96C4;&#x6570;&#x636E;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x65F6;&#xFF0C;&#x5916;&#x952E;hbook&#xFF08;&#x5373;&#x6240;&#x5C5E;&#x7684;&#x56FE;&#x4E66;&#xFF09;&#x5B57;&#x6BB5;&#x5982;&#x4F55;&#x5E8F;&#x5217;&#x5316;&#xFF1F;</p>
<p>&#x6211;&#x4EEC;&#x5148;&#x5B9A;&#x4E49;HeroInfoSerialzier&#x9664;&#x5916;&#x952E;&#x5B57;&#x6BB5;&#x5916;&#x7684;&#x5176;&#x4ED6;&#x90E8;&#x5206;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroInfoSerializer</span><span class="hljs-params">(serializers.Serializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x82F1;&#x96C4;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    GENDER_CHOICES = (
        (<span class="hljs-number">0</span>, <span class="hljs-string">&apos;male&apos;</span>),
        (<span class="hljs-number">1</span>, <span class="hljs-string">&apos;female&apos;</span>)
    )
    id = serializers.IntegerField(label=<span class="hljs-string">&apos;ID&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
    hname = serializers.CharField(label=<span class="hljs-string">&apos;&#x540D;&#x5B57;&apos;</span>, max_length=<span class="hljs-number">20</span>)
    hgender = serializers.ChoiceField(choices=GENDER_CHOICES, label=<span class="hljs-string">&apos;&#x6027;&#x522B;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    hcomment = serializers.CharField(label=<span class="hljs-string">&apos;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&apos;</span>, max_length=<span class="hljs-number">200</span>, required=<span class="hljs-keyword">False</span>, allow_null=<span class="hljs-keyword">True</span>)
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#xFF0C;&#x53EF;&#x4EE5;&#x91C7;&#x7528;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#xFF1A;</p>
<h4 id="1&#xFF09;-primarykeyrelatedfield">1&#xFF09; PrimaryKeyRelatedField</h4>
<p>&#x6B64;&#x5B57;&#x6BB5;&#x5C06;&#x88AB;&#x5E8F;&#x5217;&#x5316;&#x4E3A;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x4E3B;&#x952E;&#x3002;</p>
<pre><code class="lang-python">hbook = serializers.PrimaryKeyRelatedField(label=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
&#x6216;
hbook = serializers.PrimaryKeyRelatedField(label=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>, queryset=BookInfo.objects.all())
</code></pre>
<p>&#x6307;&#x660E;&#x5B57;&#x6BB5;&#x65F6;&#x9700;&#x8981;&#x5305;&#x542B;read_only=True&#x6216;&#x8005;queryset&#x53C2;&#x6570;&#xFF1A;</p>
<ul>
<li>&#x5305;&#x542B;read_only=True&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x8BE5;&#x5B57;&#x6BB5;&#x5C06;&#x4E0D;&#x80FD;&#x7528;&#x4F5C;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x4F7F;&#x7528;</li>
<li>&#x5305;&#x542B;queryset&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x5C06;&#x88AB;&#x7528;&#x4F5C;&#x53CD;&#x5E8F;&#x5217;&#x5316;&#x65F6;&#x53C2;&#x6570;&#x6821;&#x9A8C;&#x4F7F;&#x7528;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> booktest.serializers <span class="hljs-keyword">import</span> HeroInfoSerializer
<span class="hljs-keyword">from</span> booktest.models <span class="hljs-keyword">import</span> HeroInfo
hero = HeroInfo.objects.get(id=<span class="hljs-number">6</span>)
serializer = HeroInfoSerializer(hero)
serializer.data
<span class="hljs-comment"># {&apos;id&apos;: 6, &apos;hname&apos;: &apos;&#x4E54;&#x5CF0;&apos;, &apos;hgender&apos;: 1, &apos;hcomment&apos;: &apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;, &apos;hbook&apos;: 2}</span>
</code></pre>
<h4 id="2--stringrelatedfield">2)  StringRelatedField</h4>
<p>&#x6B64;&#x5B57;&#x6BB5;&#x5C06;&#x88AB;&#x5E8F;&#x5217;&#x5316;&#x4E3A;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;&#x65B9;&#x5F0F;&#xFF08;&#x5373;__str__&#x65B9;&#x6CD5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF09;</p>
<pre><code class="lang-python">hbook = serializers.StringRelatedField(label=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>)
</code></pre>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;</p>
<pre><code class="lang-python">{<span class="hljs-string">&apos;id&apos;</span>: <span class="hljs-number">6</span>, <span class="hljs-string">&apos;hname&apos;</span>: <span class="hljs-string">&apos;&#x4E54;&#x5CF0;&apos;</span>, <span class="hljs-string">&apos;hgender&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;hcomment&apos;</span>: <span class="hljs-string">&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;</span>, <span class="hljs-string">&apos;hbook&apos;</span>: <span class="hljs-string">&apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;</span>}
</code></pre>
<h4 id="3&#xFF09;hyperlinkedrelatedfield">3&#xFF09;HyperlinkedRelatedField</h4>
<p>&#x6B64;&#x5B57;&#x6BB5;&#x5C06;&#x88AB;&#x5E8F;&#x5217;&#x5316;&#x4E3A;&#x83B7;&#x53D6;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x6570;&#x636E;&#x7684;&#x63A5;&#x53E3;&#x94FE;&#x63A5;</p>
<pre><code class="lang-python">hbook = serializers.HyperlinkedRelatedField(label=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>, read_only=<span class="hljs-keyword">True</span>, view_name=<span class="hljs-string">&apos;books-detail&apos;</span>)
</code></pre>
<p>&#x5FC5;&#x987B;&#x6307;&#x660E;view_name&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x4FBF;DRF&#x6839;&#x636E;&#x89C6;&#x56FE;&#x540D;&#x79F0;&#x5BFB;&#x627E;&#x8DEF;&#x7531;&#xFF0C;&#x8FDB;&#x800C;&#x62FC;&#x63A5;&#x6210;&#x5B8C;&#x6574;URL&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;</p>
<pre><code class="lang-python">{<span class="hljs-string">&apos;id&apos;</span>: <span class="hljs-number">6</span>, <span class="hljs-string">&apos;hname&apos;</span>: <span class="hljs-string">&apos;&#x4E54;&#x5CF0;&apos;</span>, <span class="hljs-string">&apos;hgender&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;hcomment&apos;</span>: <span class="hljs-string">&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;</span>, <span class="hljs-string">&apos;hbook&apos;</span>: <span class="hljs-string">&apos;http://127.0.0.1:8000/books/2/&apos;</span>}
</code></pre>
<p>&#x6211;&#x4EEC;&#x6682;&#x65F6;&#x8FD8;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x89C6;&#x56FE;&#xFF0C;&#x6B64;&#x65B9;&#x5F0F;&#x4E0D;&#x518D;&#x6F14;&#x793A;&#x3002;</p>
<h4 id="4&#xFF09;slugrelatedfield">4&#xFF09;SlugRelatedField</h4>
<p>&#x6B64;&#x5B57;&#x6BB5;&#x5C06;&#x88AB;&#x5E8F;&#x5217;&#x5316;&#x4E3A;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x6307;&#x5B9A;&#x5B57;&#x6BB5;&#x6570;&#x636E;</p>
<pre><code class="lang-python">hbook = serializers.SlugRelatedField(label=<span class="hljs-string">&apos;&#x56FE;&#x4E66;&apos;</span>, read_only=<span class="hljs-keyword">True</span>, slug_field=<span class="hljs-string">&apos;bpub_date&apos;</span>)
</code></pre>
<p>slug_field&#x6307;&#x660E;&#x4F7F;&#x7528;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x54EA;&#x4E2A;&#x5B57;&#x6BB5;</p>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;</p>
<pre><code class="lang-python">{<span class="hljs-string">&apos;id&apos;</span>: <span class="hljs-number">6</span>, <span class="hljs-string">&apos;hname&apos;</span>: <span class="hljs-string">&apos;&#x4E54;&#x5CF0;&apos;</span>, <span class="hljs-string">&apos;hgender&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;hcomment&apos;</span>: <span class="hljs-string">&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;</span>, <span class="hljs-string">&apos;hbook&apos;</span>: datetime.date(<span class="hljs-number">1986</span>, <span class="hljs-number">7</span>, <span class="hljs-number">24</span>)}
</code></pre>
<h4 id="5&#xFF09;&#x4F7F;&#x7528;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;">5&#xFF09;&#x4F7F;&#x7528;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;</h4>
<pre><code class="lang-python">hbook = BookInfoSerializer()
</code></pre>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;</p>
<pre><code class="lang-python">{&apos;id&apos;: 6, &apos;hname&apos;: &apos;&#x4E54;&#x5CF0;&apos;, &apos;hgender&apos;: 1, &apos;hcomment&apos;: &apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;, &apos;hbook&apos;: OrderedDict([(&apos;id&apos;, 2), (&apos;btitle&apos;, &apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;)te&apos;, &apos;1986-07-24&apos;), (&apos;bread&apos;, 36), (&apos;bcomment&apos;, 40), (&apos;image&apos;, None)])}
</code></pre>
<h4 id="6&#xFF09;-&#x91CD;&#x5199;torepresentation&#x65B9;&#x6CD5;">6&#xFF09; &#x91CD;&#x5199;to_representation&#x65B9;&#x6CD5;</h4>
<p>&#x5E8F;&#x5217;&#x5316;&#x5668;&#x7684;&#x6BCF;&#x4E2A;&#x5B57;&#x6BB5;&#x5B9E;&#x9645;&#x90FD;&#x662F;&#x7531;&#x8BE5;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x7684;to_representation&#x65B9;&#x6CD5;&#x51B3;&#x5B9A;&#x683C;&#x5F0F;&#x7684;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x91CD;&#x5199;&#x8BE5;&#x65B9;&#x6CD5;&#x6765;&#x51B3;&#x5B9A;&#x683C;&#x5F0F;&#x3002;</p>
<p><strong>&#x6CE8;&#x610F;&#xFF0C;to_representations&#x65B9;&#x6CD5;&#x4E0D;&#x4EC5;&#x5C40;&#x9650;&#x5728;&#x63A7;&#x5236;&#x5173;&#x8054;&#x5BF9;&#x8C61;&#x683C;&#x5F0F;&#x4E0A;&#xFF0C;&#x9002;&#x7528;&#x4E8E;&#x5404;&#x4E2A;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x3002;</strong></p>
<p>&#x81EA;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookRelateField</span><span class="hljs-params">(serializers.RelatedField)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x81EA;&#x5B9A;&#x4E49;&#x7528;&#x4E8E;&#x5904;&#x7406;&#x56FE;&#x4E66;&#x7684;&#x5B57;&#x6BB5;&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_representation</span><span class="hljs-params">(self, value)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;Book: %d %s&apos;</span> % (value.id, value.btitle)
</code></pre>
<p>&#x6307;&#x660E;hbook&#x4E3A;BookRelateField&#x7C7B;&#x578B;</p>
<pre><code class="lang-python">hbook = BookRelateField(read_only=<span class="hljs-keyword">True</span>)
</code></pre>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;</p>
<pre><code class="lang-python">{<span class="hljs-string">&apos;id&apos;</span>: <span class="hljs-number">6</span>, <span class="hljs-string">&apos;hname&apos;</span>: <span class="hljs-string">&apos;&#x4E54;&#x5CF0;&apos;</span>, <span class="hljs-string">&apos;hgender&apos;</span>: <span class="hljs-number">1</span>, <span class="hljs-string">&apos;hcomment&apos;</span>: <span class="hljs-string">&apos;&#x964D;&#x9F99;&#x5341;&#x516B;&#x638C;&apos;</span>, <span class="hljs-string">&apos;hbook&apos;</span>: <span class="hljs-string">&apos;Book: 2 &#x5929;&#x9F99;&#x516B;&#x90E8;&apos;</span>}
</code></pre>
<h4 id="many&#x53C2;&#x6570;">many&#x53C2;&#x6570;</h4>
<p>&#x5982;&#x679C;&#x5173;&#x8054;&#x7684;&#x5BF9;&#x8C61;&#x6570;&#x636E;&#x4E0D;&#x662F;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#xFF0C;&#x800C;&#x662F;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x6570;&#x636E;&#xFF0C;&#x5982;&#x60F3;&#x5E8F;&#x5217;&#x5316;&#x56FE;&#x4E66;BookInfo&#x6570;&#x636E;&#xFF0C;&#x6BCF;&#x4E2A;BookInfo&#x5BF9;&#x8C61;&#x5173;&#x8054;&#x7684;&#x82F1;&#x96C4;HeroInfo&#x5BF9;&#x8C61;&#x53EF;&#x80FD;&#x6709;&#x591A;&#x4E2A;&#xFF0C;&#x6B64;&#x65F6;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;&#x7684;&#x6307;&#x660E;&#x4ECD;&#x53EF;&#x4F7F;&#x7528;&#x4E0A;&#x8FF0;&#x51E0;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x53EA;&#x662F;&#x5728;&#x58F0;&#x660E;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#x65F6;&#xFF0C;&#x591A;&#x8865;&#x5145;&#x4E00;&#x4E2A;many=True&#x53C2;&#x6570;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x6B64;&#x5904;&#x4EC5;&#x62FF;PrimaryKeyRelatedField&#x7C7B;&#x578B;&#x6765;&#x4E3E;&#x4F8B;&#xFF0C;&#x5176;&#x4ED6;&#x76F8;&#x540C;&#x3002;</p>
<p>&#x5728;BookInfoSerializer&#x4E2D;&#x6DFB;&#x52A0;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.Serializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    id = serializers.IntegerField(label=<span class="hljs-string">&apos;ID&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
    btitle = serializers.CharField(label=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>, max_length=<span class="hljs-number">20</span>)
    bpub_date = serializers.DateField(label=<span class="hljs-string">&apos;&#x53D1;&#x5E03;&#x65E5;&#x671F;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    bread = serializers.IntegerField(label=<span class="hljs-string">&apos;&#x9605;&#x8BFB;&#x91CF;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    bcomment = serializers.IntegerField(label=<span class="hljs-string">&apos;&#x8BC4;&#x8BBA;&#x91CF;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    image = serializers.ImageField(label=<span class="hljs-string">&apos;&#x56FE;&#x7247;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=<span class="hljs-keyword">True</span>, many=<span class="hljs-keyword">True</span>)  <span class="hljs-comment"># &#x65B0;&#x589E;</span>
</code></pre>
<p>&#x4F7F;&#x7528;&#x6548;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> booktest.serializers <span class="hljs-keyword">import</span> BookInfoSerializer
<span class="hljs-keyword">from</span> booktest.models <span class="hljs-keyword">import</span> BookInfo
book = BookInfo.objects.get(id=<span class="hljs-number">2</span>)
serializer = BookInfoSerializer(book)
serializer.data
<span class="hljs-comment"># {&apos;id&apos;: 2, &apos;btitle&apos;: &apos;&#x5929;&#x9F99;&#x516B;&#x90E8;&apos;, &apos;bpub_date&apos;: &apos;1986-07-24&apos;, &apos;bread&apos;: 36, &apos;bcomment&apos;: 40, &apos;image&apos;: None, &apos;heroinfo_set&apos;: [6,8, 9]}</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C03-Serializer/Declaring.html" class="navigation navigation-prev " aria-label="Previous page: 定义Serializer"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C03-Serializer/Deserializing.html" class="navigation navigation-next " aria-label="Next page: 反序列化使用"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
